local hx__shibei = fk.CreateSkill {
  name = "hx__shibei",
  tags = { Skill.Compulsory },
}

hx__shibei:addEffect(fk.Damaged, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__shibei.name) then
      local damage_events = player.room.logic:getActualDamageEvents(2, function (e)
        return e.data.to == player
      end, Player.HistoryTurn)
      if damage_events[1].data == data then
        event:setCostData(self, {choice = "recover"})
        return player:isWounded()
      else
        event:setCostData(self, {choice = "loseHp"})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__shibei.name)
    local choice = event:getCostData(self).choice
    if choice == "recover" then
      room:notifySkillInvoked(player, hx__shibei.name, "defensive")
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = hx__shibei.name,
      }
    else
      room:notifySkillInvoked(player, hx__shibei.name, "negative")
      room:loseHp(player, 1, hx__shibei.name)
    end
  end,
})

return hx__shibei